{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "from PyQt5.QtGui import QIcon\n",
    "from keras.applications.densenet import DenseNet169, preprocess_input\n",
    "from keras.models import Model\n",
    "from keras.layers import Dense, GlobalAveragePooling2D\n",
    "import numpy as np\n",
    "from keras.preprocessing import image\n",
    "img_path = '默认地址'\n",
    "pre_name = '请导入图片'\n",
    "\n",
    "\n",
    "def add_new_last_layer(base_model, nb_classes):\n",
    "    \"\"\"\n",
    "  添加最后的层\n",
    "  输入\n",
    "  base_model和分类数量\n",
    "  输出\n",
    "  新的keras的model\n",
    "  \"\"\"\n",
    "    x = base_model.output\n",
    "    x = GlobalAveragePooling2D()(x)\n",
    "    predictions = Dense(nb_classes,\n",
    "                        activation='softmax')(x)  # new softmax layer\n",
    "    model = Model(inputs=base_model.inputs, outputs=predictions)\n",
    "    return model\n",
    "\n",
    "\n",
    "def createmodel(img_path):\n",
    "    new_img = image.load_img(img_path, target_size=(150, 150))\n",
    "    img = image.img_to_array(new_img)\n",
    "    img = preprocess_input(img)\n",
    "    img = np.expand_dims(img, axis=0)\n",
    "    prediction = model.predict(img)\n",
    "    print(prediction)\n",
    "    label = [\n",
    "        'Apple___Apple_scab', 'Apple___Black_rot', 'Apple___Cedar_apple_rust',\n",
    "        'Apple___healthy', 'Blueberry___healthy',\n",
    "        'Cherry_(including_sour)___Powdery_mildew',\n",
    "        'Cherry_(including_sour)___healthy',\n",
    "        'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot',\n",
    "        'Corn_(maize)___Common_rust_', 'Corn_(maize)___Northern_Leaf_Blight',\n",
    "        'Corn_(maize)___healthy', 'Grape___Black_rot',\n",
    "        'Grape___Esca_(Black_Measles)',\n",
    "        'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)', 'Grape___healthy',\n",
    "        'Orange___Haunglongbing_(Citrus_greening)', 'Peach___Bacterial_spot',\n",
    "        'Peach___healthy', 'Pepper,_bell___Bacterial_spot',\n",
    "        'Pepper,_bell___healthy', 'Potato___Early_blight',\n",
    "        'Potato___Late_blight', 'Potato___healthy', 'Raspberry___healthy',\n",
    "        'Soybean___healthy', 'Squash___Powdery_mildew',\n",
    "        'Strawberry___Leaf_scorch', 'Strawberry___healthy',\n",
    "        'Tomato___Bacterial_spot', 'Tomato___Early_blight',\n",
    "        'Tomato___Late_blight', 'Tomato___Leaf_Mold',\n",
    "        'Tomato___Septoria_leaf_spot',\n",
    "        'Tomato___Spider_mites Two-spotted_spider_mite',\n",
    "        'Tomato___Target_Spot', 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',\n",
    "        'Tomato___Tomato_mosaic_virus', 'Tomato___healthy'\n",
    "    ]\n",
    "    a = prediction.flatten()\n",
    "    b = a.max()\n",
    "    for index, item in enumerate(a):\n",
    "        if item == b:\n",
    "            class_name = label[index]\n",
    "    print(class_name)\n",
    "    return class_name\n",
    "\n",
    "\n",
    "from PyQt5 import QtWidgets, QtGui\n",
    "import sys\n",
    "from plant import Ui_MainWindow\n",
    "from PyQt5.QtWidgets import QFileDialog, QMessageBox\n",
    "\n",
    "\n",
    "class mywindow(QtWidgets.QMainWindow, Ui_MainWindow):\n",
    "    def __init__(self):\n",
    "        super(mywindow, self).__init__()\n",
    "        self.setupUi(self)\n",
    "\n",
    "    def imageopen(self):\n",
    "        # 打开文件路径\n",
    "        # 设置文件扩展名过滤,注意用双分号间隔\n",
    "        imgName, imgType = QFileDialog.getOpenFileName(\n",
    "            self, \"打开图片\", \"\", \" *.jpg;;*.png;;*.jpeg;;*.bmp;;All Files (*)\")\n",
    "        img_path = imgName\n",
    "        png = QtGui.QPixmap(imgName).scaled(self.label_2.width(),\n",
    "                                            self.label_2.height())\n",
    "        self.label_2.setPixmap(png)\n",
    "        self.textEdit.setText(img_path)\n",
    "        # print(img_path)\n",
    "        # print(self.textEdit.toPlainText())\n",
    "        # img_path是全局变量，但是经过测试并没有传递参数，仿佛只在点击的那一刻传递了\n",
    "\n",
    "    def pre(self):\n",
    "        if self.textEdit.toPlainText():\n",
    "            s = self.textEdit.toPlainText()\n",
    "            if u'\\u4e00' <= s[0] <= u'\\u9fff':\n",
    "                QMessageBox.setWindowIcon(self, QIcon('./images/jg.png'))\n",
    "                QMessageBox.about(self, \"警告\", \"请重新导入病害图像！\")\n",
    "            else:\n",
    "                pre_name = createmodel(self.textEdit.toPlainText())\n",
    "                label = [\n",
    "                    'Apple___Apple_scab', 'Apple___Black_rot',\n",
    "                    'Apple___Cedar_apple_rust', 'Apple___healthy',\n",
    "                    'Blueberry___healthy',\n",
    "                    'Cherry_(including_sour)___Powdery_mildew',\n",
    "                    'Cherry_(including_sour)___healthy',\n",
    "                    'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot',\n",
    "                    'Corn_(maize)___Common_rust_',\n",
    "                    'Corn_(maize)___Northern_Leaf_Blight',\n",
    "                    'Corn_(maize)___healthy', 'Grape___Black_rot',\n",
    "                    'Grape___Esca_(Black_Measles)',\n",
    "                    'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)',\n",
    "                    'Grape___healthy',\n",
    "                    'Orange___Haunglongbing_(Citrus_greening)',\n",
    "                    'Peach___Bacterial_spot', 'Peach___healthy',\n",
    "                    'Pepper,_bell___Bacterial_spot', 'Pepper,_bell___healthy',\n",
    "                    'Potato___Early_blight', 'Potato___Late_blight',\n",
    "                    'Potato___healthy', 'Raspberry___healthy',\n",
    "                    'Soybean___healthy', 'Squash___Powdery_mildew',\n",
    "                    'Strawberry___Leaf_scorch', 'Strawberry___healthy',\n",
    "                    'Tomato___Bacterial_spot', 'Tomato___Early_blight',\n",
    "                    'Tomato___Late_blight', 'Tomato___Leaf_Mold',\n",
    "                    'Tomato___Septoria_leaf_spot',\n",
    "                    'Tomato___Spider_mites Two-spotted_spider_mite',\n",
    "                    'Tomato___Target_Spot',\n",
    "                    'Tomato___Tomato_Yellow_Leaf_Curl_Virus',\n",
    "                    'Tomato___Tomato_mosaic_virus', 'Tomato___healthy'\n",
    "                ]\n",
    "                if pre_name == label[0]:\n",
    "                    self.textEdit.setText('预测结果为：苹果黑星病')\n",
    "                elif pre_name == label[1]:\n",
    "                    self.textEdit.setText('预测结果为：苹果黑腐病')\n",
    "                elif pre_name == label[2]:\n",
    "                    self.textEdit.setText('预测结果为：苹果雪松锈病')\n",
    "                elif pre_name == label[3]:\n",
    "                    self.textEdit.setText('预测结果为：苹果健康')\n",
    "                elif pre_name == label[4]:\n",
    "                    self.textEdit.setText('预测结果为：蓝莓健康')\n",
    "                elif pre_name == label[5]:\n",
    "                    self.textEdit.setText('预测结果为：樱桃白粉病')\n",
    "                elif pre_name == label[6]:\n",
    "                    self.textEdit.setText('预测结果为：樱桃健康')\n",
    "                elif pre_name == label[7]:\n",
    "                    self.textEdit.setText('预测结果为：玉米灰斑病')\n",
    "                elif pre_name == label[8]:\n",
    "                    self.textEdit.setText('预测结果为：玉米普通锈病')\n",
    "                elif pre_name == label[9]:\n",
    "                    self.textEdit.setText('预测结果为：玉米大斑病')\n",
    "                elif pre_name == label[10]:\n",
    "                    self.textEdit.setText('预测结果为：玉米健康')\n",
    "                elif pre_name == label[11]:\n",
    "                    self.textEdit.setText('预测结果为：葡萄黑腐病')\n",
    "                elif pre_name == label[12]:\n",
    "                    self.textEdit.setText('预测结果为：葡萄轮斑病')\n",
    "                elif pre_name == label[13]:\n",
    "                    self.textEdit.setText('预测结果为：葡萄褐斑病')\n",
    "                elif pre_name == label[14]:\n",
    "                    self.textEdit.setText('预测结果为：葡萄健康')\n",
    "                elif pre_name == label[15]:\n",
    "                    self.textEdit.setText('预测结果为：柑橘黄龙病')\n",
    "                elif pre_name == label[16]:\n",
    "                    self.textEdit.setText('预测结果为：桃细菌黑斑病')\n",
    "                elif pre_name == label[17]:\n",
    "                    self.textEdit.setText('预测结果为：桃健康')\n",
    "                elif pre_name == label[18]:\n",
    "                    self.textEdit.setText('预测结果为：胡椒细菌性叶斑病')\n",
    "                elif pre_name == label[19]:\n",
    "                    self.textEdit.setText('预测结果为：胡椒健康')\n",
    "                elif pre_name == label[20]:\n",
    "                    self.textEdit.setText('预测结果为：马铃薯早疫病')\n",
    "                elif pre_name == label[21]:\n",
    "                    self.textEdit.setText('预测结果为：马铃薯晚疫病')\n",
    "                elif pre_name == label[22]:\n",
    "                    self.textEdit.setText('预测结果为：马铃薯健康')\n",
    "                elif pre_name == label[23]:\n",
    "                    self.textEdit.setText('预测结果为：树莓健康')\n",
    "                elif pre_name == label[24]:\n",
    "                    self.textEdit.setText('预测结果为：大豆健康')\n",
    "                elif pre_name == label[25]:\n",
    "                    self.textEdit.setText('预测结果为：南瓜白粉病')\n",
    "                elif pre_name == label[26]:\n",
    "                    self.textEdit.setText('预测结果为：草莓叶焦病')\n",
    "                elif pre_name == label[27]:\n",
    "                    self.textEdit.setText('预测结果为：草莓健康')\n",
    "                elif pre_name == label[28]:\n",
    "                    self.textEdit.setText('预测结果为：番茄疮痂病')\n",
    "                elif pre_name == label[29]:\n",
    "                    self.textEdit.setText('预测结果为：番茄早疫病')\n",
    "                elif pre_name == label[30]:\n",
    "                    self.textEdit.setText('预测结果为：番茄晚疫病')\n",
    "                elif pre_name == label[31]:\n",
    "                    self.textEdit.setText('预测结果为：番茄叶霉病')\n",
    "                elif pre_name == label[32]:\n",
    "                    self.textEdit.setText('预测结果为：番茄斑枯病')\n",
    "                elif pre_name == label[33]:\n",
    "                    self.textEdit.setText('预测结果为：番茄红蜘蛛损伤')\n",
    "                elif pre_name == label[34]:\n",
    "                    self.textEdit.setText('预测结果为：番茄斑点病')\n",
    "                elif pre_name == label[35]:\n",
    "                    self.textEdit.setText('预测结果为：番茄黄化曲叶病毒病')\n",
    "                elif pre_name == label[36]:\n",
    "                    self.textEdit.setText('预测结果为：番茄花叶病毒病')\n",
    "                elif pre_name == label[37]:\n",
    "                    self.textEdit.setText('预测结果为：番茄健康')\n",
    "        else:\n",
    "            QMessageBox.setWindowIcon(self, QIcon('./images/jg.png'))\n",
    "            QMessageBox.about(self, \"警告\", \"请从本地导入病害图像！\")\n",
    "\n",
    "\n",
    "model = DenseNet169(input_shape=(150, 150, 3), include_top=False)\n",
    "model = add_new_last_layer(model, 38)\n",
    "print(\"模型建立成功\")\n",
    "model.load_weights('./h5/DenseNet169.hdf5')\n",
    "print(\"权重导入成功\")\n",
    "app = QtWidgets.QApplication(sys.argv)\n",
    "window = mywindow()\n",
    "window.setStyleSheet(\"#MainWindow{border-image:url(./images/b1.jpg);}\")\n",
    "\n",
    "window.show()\n",
    "sys.exit(app.exec_())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf-gpu",
   "language": "python",
   "name": "tf-gpu"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
